package org.mch.comm.db.utils;

import java.io.Serializable;
import java.util.List;

public class SQLHelper implements Serializable {
	private static final long serialVersionUID = 1L;

	public static String getLIKESQL(String field, List<String> list) {
		if ((list == null) || (list.size() <= 0)) {
			return "";
		}

		return getLIKESQL(field, (String[]) list.toArray(new String[0]));
	}

	public static String getLIKESQL(String field, String[] strs) {
		if ((strs == null) || (strs.length <= 0)) {
			return "";
		}

		StringBuffer sql = new StringBuffer(" ");
		for (int i = 0; i < strs.length; i++) {
			if (i == 0) {
				sql.append(field);
				sql.append(" LIKE '%");
				sql.append(strs[i]);
				sql.append("' ");
			} else {
				sql.append(" OR ");
				sql.append(field);
				sql.append(" LIKE '%");
				sql.append(strs[i]);
				sql.append("' ");
			}
		}

		return sql.toString();
	}

	public static String getINSQL(String field, String[] strs) {
		if ((strs == null) || (strs.length <= 0)) {
			return "";
		}

		StringBuffer sql = new StringBuffer("(");
		for (int i = 0; i < strs.length; i++) {
			if (i == 0) {
				sql.append(field);
				sql.append(" IN (");
				sql.append("'");
				sql.append(strs[i]);
				sql.append("'");
			} else if (i % 200 == 0) {
				sql.append(") OR ");
				sql.append(field);
				sql.append(" IN (");
				sql.append("'");
				sql.append(strs[i]);
				sql.append("'");
			} else {
				sql.append(",'");
				sql.append(strs[i]);
				sql.append("'");
			}

			if (i == strs.length - 1) {
				sql.append(")");
			}
		}
		sql.append(")");

		return sql.toString();
	}

	public static String getINSQL(String field, List<String> list) {
		if ((list == null) || (list.size() <= 0)) {
			return "";
		}

		return getINSQL(field, (String[]) list.toArray(new String[0]));
	}
}